import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class MyDialog extends StatefulWidget {
  String title;
  String content;
  MyDialog({this.title='空',this.content='空'});

  @override
  State<StatefulWidget> createState() => MyDialogState(title=this.title,content=this.content);
}

class MyDialogState extends State {
  String title;
  String content;
  MyDialogState(String title, String content);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('MyDialog')),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                RaisedButton(
                  elevation: 5,
                  textColor: Colors.white,
                  color: Colors.blue,
                  child: Text('11'),
                  onPressed: _alertDialog,
                ),
              ],
            ),
            SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                RaisedButton(
                  elevation: 5,
                  textColor: Colors.white,
                  color: Colors.blue,
                  child: Text('22'),
                  onPressed: _simpleDialog,
                ),
              ],
            ),
            SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                RaisedButton(
                  elevation: 5,
                  textColor: Colors.white,
                  color: Colors.blue,
                  child: Text('33'),
                  onPressed: _modalBottomSheet,
                ),
              ],
            ),
            SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                RaisedButton(
                  elevation: 5,
                  textColor: Colors.white,
                  color: Colors.blue,
                  child: Text('44'),
                  onPressed: _Toast,
                ),
              ],
            ),
            SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                RaisedButton(
                  elevation: 5,
                  textColor: Colors.white,
                  color: Colors.blue,
                  child: Text('自定义Dialog'),
                  onPressed: (){
                    print(this.title);
                    print(this.content);
//                      _MyDialog(this.title,this.content);
                    },
                ),
              ],
            ),
          ],
        ));
  }

  _alertDialog() async {
    var res = await showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
            title: Text('提示信息'),
            content: Text('您确定要删除吗？'),
            actions: [
              FlatButton(
                child: Text('取消'),
                onPressed: () {
                  Navigator.pop(context, 'cancel');
                },
              ),
              FlatButton(
                child: Text('确定'),
                onPressed: () {
                  Navigator.pop(context, 'ok');
                },
              ),
            ]);
      },
    );
    print(res);
  }

  _simpleDialog() async {
    var res = await showDialog(
      context: context,
      builder: (context) {
        return SimpleDialog(title: Text('选择内容'), children: [
          SimpleDialogOption(
            child: Text('option1'),
            onPressed: () {
              Navigator.pop(context, 'option1');
            },
          ),
          Divider(),
          SimpleDialogOption(
            child: Text('option2'),
            onPressed: () {
              Navigator.pop(context, 'option2');
            },
          ),
          Divider(),
          SimpleDialogOption(
            child: Text('option3'),
            onPressed: () {
              Navigator.pop(context, 'option3');
            },
          ),
          Divider(),
          SimpleDialogOption(
            child: Text('option4'),
            onPressed: () {
              Navigator.pop(context, 'option4');
            },
          ),
        ]);
      },
    );
    print('22 $res');
  }

  _modalBottomSheet() async {
    var res = await showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            height: 150,
            child: ListView(
              children: [
                ListTile(
                  title: Text('option1'),
                  onTap: () {
                    Navigator.pop(context, 'option1');
                  },
                ),
                Divider(),
                ListTile(
                  title: Text('option2'),
                  onTap: () {
                    Navigator.pop(context, 'option2');
                  },
                ),
                Divider(),
                ListTile(
                  title: Text('option3'),
                  onTap: () {
                    Navigator.pop(context, 'option3');
                  },
                ),
              ],
            ),
          );
        });
    print('33 $res');
  }

  _Toast() {
    Fluttertoast.showToast(
        msg: "44444",
        toastLength: Toast.LENGTH_LONG,
        gravity: ToastGravity.TOP,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.yellow,
        textColor: Colors.red,
        fontSize: 16.0);
  }

  _MyDialog(title,content) {
    showDialog(
      context: context,
      builder: (context) {
        return Material(
            type: MaterialType.transparency,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  height: 200,
                  width: 200,
                  color: Colors.white,
                  child: Column(
                    children: [
                      Padding(
                        padding: EdgeInsets.all(10),
                        child: Stack(children: [
                          Align(
                              alignment: Alignment.topCenter,
                              child: Text(title)),
                          Align(
                              alignment: Alignment.topRight,
                              child: Icon(Icons.close)),
                        ]),
                      ),
                      Divider(),
                      Padding(
                        padding: EdgeInsets.all(10),
                        child: Container(
                          alignment: Alignment.topLeft,
                          child: Text(content),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ));
      },
    );
  }
}
